package com.yogee.topseller.activity.my;


import android.Manifest;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.ms.square.android.expandabletextview.ExpandableTextView;
import com.umeng.analytics.social.UMPlatformData;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.ShareContent;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;
import com.umeng.socialize.utils.Log;
import com.yogee.core.base.HttpActivity;
import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.ProductDetailsActivity;
import com.yogee.topseller.activity.login.model.bean.AboutUsBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.AppUtil;
import com.yogee.topseller.utils.GlideUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AboutUsActivity extends HttpActivity {


    @BindView(R.id.title_white_left_back)
    LinearLayout titleWhiteLeftBack;
    @BindView(R.id.title_white_title)
    TextView titleWhiteTitle;
    @BindView(R.id.title_white_right_txt)
    TextView titleWhiteRightTxt;
    @BindView(R.id.expand_text_view)
    ExpandableTextView expandTextView;
    @BindView(R.id.iv_qr_code)
    ImageView ivQrCode;

    private PopupWindow popupWindow;

    private String shareTitle,shareIcon,shareImg,shareUrl,shareContentTxt;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_about_us;
    }

    @Override
    protected void initView() {
        loadAboutInfo();
        titleWhiteTitle.setText("关于我们");
        titleWhiteRightTxt.setText("推荐给好友");
        titleWhiteRightTxt.setVisibility(View.VISIBLE);
        titleWhiteRightTxt.setTextColor(Color.parseColor("#ffffff"));

        if (Build.VERSION.SDK_INT >= 23) {
            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS};
            ActivityCompat.requestPermissions(this, mPermissionList, 123);
        }

        shareTitle = "搜搜出单";//分享标题
        shareUrl = "http://www.baidu.com";//分享目标路径
        shareContentTxt = "内容内容内容-----内容内容内容";//分享内容
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        UMShareAPI.get(this).release();
        if(Build.VERSION.SDK_INT>=23){
            String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.ACCESS_FINE_LOCATION,Manifest.permission.CALL_PHONE,Manifest.permission.READ_LOGS,Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.SET_DEBUG_APP,Manifest.permission.SYSTEM_ALERT_WINDOW,Manifest.permission.GET_ACCOUNTS,Manifest.permission.WRITE_APN_SETTINGS};
            ActivityCompat.requestPermissions(this,mPermissionList,123);
        }
        expandTextView.setText("北京晟西奇想科技有限公司于 2017 年 7 月在北京成立，是一家以金融大数据、智能\n" +
                "AI 信息技术金融产品实时查询平台。旗下自主研发的搜搜出单于同年 10 月上线测试，\n" +
                "2018 年 2 月近百人的大数据技术中心成立。同年 5 月获得天津仁爱集团数千万天使轮融资。目前“搜搜出单”注册用户100000+、合作资产管理机构1500+、数据库中交互金融资产数据 50000+、关联融资企业信息 100000+。\n" +
                "搜搜出单依托近百人的数据技术团队并与知名院校技术合作等方式，通过线上系统智能采集和线下资产机构深度合作双线发展的模式，致力于打造第一家金融垂直领域金融产品大数据查询平台。为金融销售、高净值人群、投行人员、金融机构、资产管理机构提供专业、全面的金融产品大数据查询工具");
    }

    @OnClick({R.id.title_white_left_back, R.id.title_white_right_txt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_white_left_back:
                finish();
                break;
            case R.id.title_white_right_txt:
                showBottomPopuWindow();
                break;
        }
    }

    private void showBottomPopuWindow() {
        LinearLayout rootView = (LinearLayout) LayoutInflater.from(this).inflate(
                R.layout.dialog_share_board, null);

        popupWindow = new PopupWindow(rootView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        TextView cancelT = (TextView) rootView.findViewById(R.id.btn_cancel);
        cancelT.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        LinearLayout wechatL = (LinearLayout) rootView.findViewById(R.id.share_wechat);
        wechatL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AppUtil.isFastDoubleClick(1000)) {
                    return;
                }
                doShare(0);
                popupWindow.dismiss();
            }
        });
        LinearLayout wxCircleL = (LinearLayout) rootView.findViewById(R.id.share_wxcircle);
        wxCircleL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AppUtil.isFastDoubleClick(1000)) {
                    return;
                }
                doShare(1);
                popupWindow.dismiss();
            }
        });
        LinearLayout sinaL = (LinearLayout) rootView.findViewById(R.id.share_sina);
        sinaL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AppUtil.isFastDoubleClick(1000)) {
                    return;
                }
                doShare(2);
                popupWindow.dismiss();
            }
        });
        LinearLayout emailL = (LinearLayout) rootView.findViewById(R.id.share_email);
        emailL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (AppUtil.isFastDoubleClick(1000)) {
                    return;
                }
                doShare(3);
                popupWindow.dismiss();
            }
        });
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setOutsideTouchable(true);
        popupWindow.setTouchable(true);
        popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
        popupWindow.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
    }

    private void doShare(int type) {
        switch (type) {
            case 0:
                if (AppUtil.isWeixinAvilible(AboutUsActivity.this)){
                    share(SHARE_MEDIA.WEIXIN);
                }
                break;
            case 1:
                if (AppUtil.isWeixinAvilible(AboutUsActivity.this)){
                    share(SHARE_MEDIA.WEIXIN_CIRCLE);
                }
                break;
            case 2:
                if (AppUtil.isQQClientAvailable(AboutUsActivity.this)){
                    share(SHARE_MEDIA.QQ);
                }
                break;
            case 3:
                if (AppUtil.isQQClientAvailable(AboutUsActivity.this)){
                    share(SHARE_MEDIA.QZONE);
                }
                break;
        }
    }

    private void share(SHARE_MEDIA plarform){
        //分享存图片
        UMImage thumb = new UMImage(AboutUsActivity.this, shareImg);
        new ShareAction(AboutUsActivity.this)
                .withMedia (thumb)
                .setPlatform (plarform)
                .setCallback (umShareListener)
                .share ();
    }


    private UMShareListener umShareListener = new UMShareListener() {
        @Override
        public void onStart(SHARE_MEDIA platform) {
            //分享开始的回调
        }

        @Override
        public void onResult(SHARE_MEDIA platform) {
            Log.d("plat", "platform" + platform);
//            ToastUtils.showToast(AboutUsActivity.this, " 分享成功啦");

        }

        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            ToastUtils.showToast(AboutUsActivity.this," 分享失败啦");
            if (t != null) {
                Log.d("throw", "throw:" + t.getMessage());
            }
        }

        @Override
        public void onCancel(SHARE_MEDIA platform) {
            ToastUtils.showToast(AboutUsActivity.this, " 分享取消了");
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    private void loadAboutInfo() {
        HttpManager.getInstance().loadAboutUs().compose(this.bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<AboutUsBean>() {
            @Override
            public void onNext(AboutUsBean o) {
                if ("1".equals(o.getResult())) {
//                    expandTextView.setText(o.getContent());
                    GlideUtils.defultImage(AboutUsActivity.this, o.getQrCode(),ivQrCode );
                    shareImg = o.getQrCode();//设置分享图片
                }
            }
        }, this));
    }

}
